package top.llove;

import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException;
import org.eclipse.jgit.api.errors.JGitInternalException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import top.llove.utils.res.ApiResult;

import javax.servlet.http.HttpServletRequest;
import java.io.FileNotFoundException;


@RestControllerAdvice
@Slf4j
public class MyExceptionHandler {




    /**
     * 空指针异常
     * @author: 幸会
     * @date: 2021/6/7 9:18
     * @description: nullPointerException
     * @param e
     * @param req
     * @return {@link ApiResult}
     */
    @ExceptionHandler(value = NullPointerException.class)
    public ApiResult nullPointerException(Exception e, HttpServletRequest req) {
        e.printStackTrace();
        log.error("url: " + req.getRequestURI() + ", msg: " + e.getMessage());
        return ApiResult.fail(500, e.getMessage(), null);
    }

    /**
     * 数字格式化异常
     * @author: 幸会
     * @date: 2021/6/7 9:19
     * @description: numberFormatException
     * @param e
     * @param req
     * @return {@link ApiResult}
     */
    @ExceptionHandler(value = NumberFormatException.class)
    public ApiResult numberFormatException(Exception e, HttpServletRequest req) {
        e.printStackTrace();
        log.error("url: " + req.getRequestURI() + ", msg: " + e.getMessage());
        return ApiResult.fail(500, e.getMessage(), null);
    }


    /**
     * 文件丢失异常
     * @author: 幸会
     * @date: 2021/6/7 9:19
     * @description: fileNotFoundException
     * @param e
     * @param req
     * @return {@link ApiResult}
     */
    @ExceptionHandler(value = FileNotFoundException.class)
    public ApiResult fileNotFoundException(Exception e, HttpServletRequest req) {
        e.printStackTrace();
        log.error("url: " + req.getRequestURI() + ", msg: " + e.getMessage());
        return ApiResult.fail(404, e.getMessage(), null);
    }

    /**
     * Git操作异常
     * @param e
     * @param req
     * @return
     */
    @ExceptionHandler(value = JGitInternalException.class)
    public ApiResult jGitInternalException (Exception e, HttpServletRequest req) {
        e.printStackTrace();
        log.error("url: " + req.getRequestURI() + ", msg: " + e.getMessage());
        return ApiResult.fail(500, e.getMessage(), false);
    }

    /**
     * 文件大小超出异常
     * @param e
     * @param req
     * @return
     */
    @ExceptionHandler(value = FileSizeLimitExceededException.class)
    public ApiResult fileSizeLimitExceededException (Exception e, HttpServletRequest req) {
        e.printStackTrace();
        log.error("url: " + req.getRequestURI() + ", msg: " + e.getMessage());
        return ApiResult.fail(500, e.getMessage(), "文件大小超出限制");
    }


    /**
     * 非法数据异常
     * @param e
     * @param req
     * @return
     */
    @ExceptionHandler(value = IllegalArgumentException.class)
    public ApiResult illegalArgumentException(Exception e, HttpServletRequest req) {
        e.printStackTrace();
        log.warn("url: " + req.getRequestURI() + ", msg: " + e.getMessage());
        return ApiResult.fail(500, "非法数据异常", e.getMessage());
    }
}
